package com.mdp.workflow.biz.hi.ctrl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.entity.LangTips;
import com.mdp.core.entity.Result;
import com.mdp.core.err.BizException;
import com.mdp.core.query.QueryTools;
import com.mdp.core.utils.RequestUtils;
import com.mdp.safe.client.entity.User;
import com.mdp.safe.client.utils.LoginUtils;
import com.mdp.swagger.ApiEntityParams;
import com.mdp.workflow.api.FlowProcinstService;
import com.mdp.workflow.biz.hi.entity.Procinst;
import com.mdp.workflow.biz.hi.mapper.ProcinstMapper;
import com.mdp.workflow.biz.hi.service.ProcinstService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value="/*/hi/procinst")
@Api(tags={"act_hi_procinst-操作接口"})
public class ProcinstController {
	
	static Logger logger =LoggerFactory.getLogger(ProcinstController.class);
	
	@Autowired
	private FlowProcinstService procinstService;
	@Autowired
	ProcinstMapper procinstMapper;



	@Value(value = "${mdp.platform-branch-id:platform-branch-001}")
	String platformBranchId;

	@ApiOperation( value = "act_hi_procinst-查询列表",notes=" ")
	@ApiEntityParams(Procinst.class)
	@ApiResponses({
		@ApiResponse(code = 200,response=Procinst.class,message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'错误码'},total:总记录数,data:[数据对象1,数据对象2,...]}")
	})
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public Result listProcinst(@ApiIgnore @RequestParam Map<String,Object> params){
		try {
			User user=LoginUtils.getCurrentUserInfo();
			RequestUtils.transformArray(params, "categoryIds");
			RequestUtils.transformArray(params, "tagIds");
			RequestUtils.transformArray(params, "procInstIds");
			QueryTools.alias(params,"tenantId res.tenant_id_");
			QueryWrapper<Procinst> qw = QueryTools.initQueryWrapper(Procinst.class , params,"res.*","pd.*","p.*").in("res.tenant_id_",platformBranchId,user.getBranchId());
			IPage page=QueryTools.initPage(params);
			List<Map<String,Object>> datas = procinstService.selectListMapByWhere(page,qw,params);
			return Result.ok("query-ok","查询成功").setData(datas).setTotal(page.getTotal());
		}catch (BizException e) {
			return Result.error(e);
		}catch (Exception e) {
			return Result.error(e);
		}
	}


	@ApiOperation( value = "查询一个",notes=" ")
	@RequestMapping(value="/queryById",method=RequestMethod.GET)
	public Result queryById(@ApiIgnore @RequestParam Map<String,Object> params){
		Assert.notNull(params.get("id"));
		Procinst procinst=procinstMapper.selectById((Serializable) params.get("id"));
		 return Result.ok().setData(procinst);
	}

	@ApiOperation( value = "act_hi_procinst-删除",notes=" ")
	@ApiResponses({
		@ApiResponse(code = 200, message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'失败时错误码'}}")
	}) 
	@RequestMapping(value="/del",method=RequestMethod.POST)
	public Result delProcinst(@RequestBody Procinst procinst){
		User user=LoginUtils.getCurrentUserInfo();
		procinstService.deleteProcessInstance(procinst.getId(),user.getUserid()+":"+user.getUsername()+"删除");
        return Result.ok("del-ok","删除成功！");
	}

}
